↜ Back to index Introduction to Numerical Analysis 1

Solution Lecture a6

Exercise 2

! Implementation of the midpoint method for the Lotka-Volterra system
implicit none
integer i, n
real h, t
real y(2), k(2)

h = 0.1
n = 100 / h

! Initial data
y = [10., 10.]
print *, 0., y

do i = 1, n
    k = y + 0.5 * h * [1.1 * y(1) - 0.4 * y(1) * y(2), 0.1 * y(1) * y(2) - 0.4 * y(2)]
    y = y + h * [1.1 * k(1) - 0.4 * k(1) * k(2), 0.1 * k(1) * k(2) - 0.4 * k(2)]

    print *, i * h, y
enddo

end

Exercise 3

! Implementation of the midpoint method for the Lotka-Volterra system
! and finding the minimum and maximum of x and y
implicit none
integer i, j, n
real h, t
real y(2), k(2), ymin(2), ymax(2)

h = 0.1
n = 100 / h

! Initial data
y = [10., 10.]
ymax = y
ymin = y

do i = 1, n
    k = y + 0.5 * h * [1.1 * y(1) - 0.4 * y(1) * y(2), 0.1 * y(1) * y(2) - 0.4 * y(2)]
    y = y + h * [1.1 * k(1) - 0.4 * k(1) * k(2), 0.1 * k(1) * k(2) - 0.4 * k(2)]

    do j = 1, 2
        ymax(j) = max(ymax(j), y(j))
        ymin(j) = min(ymin(j), y(j))
    enddo
enddo

print *, 'Minimum of x and y:', ymin
print *, 'Maximum of x and y:', ymax

end